#include "validatecard.h"
#include <iostream>
#include <parksystem.h>
using namespace std;

ValidateCard::ValidateCard()
{
    //initial mysql datesource profile
    dbConn = QSqlDatabase::addDatabase("QSQLITE");
    dbConn.setDatabaseName("db_parkid");
    qDebug() << dbConn.open();
    query = QSqlQuery(dbConn);

}
Card* ValidateCard::initialDataSource(){

    QString detectiveDataSource = "select * from card";
    query.exec(detectiveDataSource);
    int position = 0;
    while(query.next()){

        if(query.value(1).toBool() == true){

            int cardNum = query.value(0).toInt();
            QString cardIdString;
            if(cardNum >= 10)
                cardIdString = QString("0000%1").arg(cardNum);
            else
                cardIdString = QString("00000%1").arg(cardNum);

            InitialPark[position].setCardNum(cardIdString);
            InitialPark[position].setStatus(query.value(1).toBool());
            InitialPark[position].setTaken(query.value(2).toInt());

            position++;
        }
    }
    InitialPark[position].setCardNum("ZZZZZZ");
    InitialPark[position].setStatus(false);
    InitialPark[position].setTaken(-100);

    qDebug() << QString("get test of =:%1").arg(InitialPark[position].getTaken());
    return InitialPark;

}


bool ValidateCard::parkCar(Card card){
    QString parkCarQuery = "update card set status = true,taken = " + QString("%1").arg(card.getTaken())+" where cardNum=\""+card.getCardNum()+"\"";
    query.exec(parkCarQuery);
    return true;
}

bool ValidateCard::leaveCar(Card card){
    QString leaveCarQuery = "update card set status = false,taken = -1 where cardNum = \""+card.getCardNum()+"\"";
    query.exec(leaveCarQuery);
    return true;
}

bool ValidateCard::checkCardValidation(Card card){

    QString str = "select * from card where cardNum = \""+card.getCardNum() + "\"";
    query.exec(str);
    QString cardNum;
    bool    cardStatus = false;
    if(query.next()){
        cardNum    = query.value(1).toString();
        cardStatus = query.value(5).toBool();
    }else{
        qDebug() << "cardNum does not exist..";
        return false;
    }

    if(cardNum == card.getCardNum() && cardStatus == card.getStatus())
    {
        return true;
    }else{
        return false;
    }
}

int ValidateCard::getTakenByCardNum(QString cardNum){

    Card *card = getCardByCardNum(cardNum);
    return card->getTaken();

}

Card *ValidateCard::getCardByCardNum(QString cardNum){
    QString validateCardQuery = "select * from card where cardNum = \"" + cardNum + "\" ";
    qDebug() << "get card info:" << validateCardQuery;
    query.exec(validateCardQuery);
    if(query.next()){
        return new Card(
                    query.value(0).toInt(),
                    query.value(1).toString(),
                    query.value(2).toString(),
                    query.value(3).toString(),
                    query.value(4).toInt(),
                    query.value(5).toBool()
                    );
    }else{
        return new Card();
    }
}

bool ValidateCard::validateLeave(Card * card){

    QString validateCardQuery = "select * from card where cardNum =\"" + card->getCardNum() + "\"";
    query.exec(validateCardQuery);
    QString carNum;
    bool    cardStatus = true;

    if(query.next()){
        carNum     = query.value(1).toString();
        qDebug() << query.value(1).toString();
        cardStatus = query.value(5).toBool();
        qDebug() << query.value(5).toBool();
    }else{
        return false;
    }

    if(carNum == card->getCardNum() && cardStatus == card->getStatus())
    {
        return true;
    }else{
        return false;
    }
}
void ValidateCard::initialNull(){
    QString validateCardQuery = "update card set taken=-1,status=false;";
    query.exec(validateCardQuery);
}
